<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>RF22: RF22Router.h Source File</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />



</head>
<body>
<div id="top"><!-- do not remove this div! -->


<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">RF22
   
   </div>
   
  </td>
  
  
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.5.1 -->
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
    </ul>
  </div>
<div class="header">
  <div class="headertitle">
<div class="title">RF22Router.h</div>  </div>
</div>
<div class="contents">
<div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">// RF22Router.h</span>
<a name="l00002"></a>00002 <span class="comment">//</span>
<a name="l00003"></a>00003 <span class="comment">// Author: Mike McCauley (mikem@open.com.au)</span>
<a name="l00004"></a>00004 <span class="comment">// Copyright (C) 2011 Mike McCauley</span>
<a name="l00005"></a>00005 <span class="comment">// $Id: RF22Router.h,v 1.7 2011/02/15 01:18:03 mikem Exp $</span>
<a name="l00006"></a>00006 
<a name="l00007"></a>00007 <span class="preprocessor">#ifndef RF22Router_h</span>
<a name="l00008"></a>00008 <span class="preprocessor"></span><span class="preprocessor">#define RF22Router_h</span>
<a name="l00009"></a>00009 <span class="preprocessor"></span>
<a name="l00010"></a>00010 <span class="preprocessor">#include &lt;RF22ReliableDatagram.h&gt;</span>
<a name="l00011"></a>00011 
<a name="l00012"></a>00012 <span class="comment">// Default max number of hops we will route</span>
<a name="l00013"></a>00013 <span class="preprocessor">#define RF22_DEFAULT_MAX_HOPS 30</span>
<a name="l00014"></a>00014 <span class="preprocessor"></span>
<a name="l00015"></a>00015 <span class="comment">// The default size of the routing table we keep</span>
<a name="l00016"></a>00016 <span class="preprocessor">#define RF22_ROUTING_TABLE_SIZE 10</span>
<a name="l00017"></a>00017 <span class="preprocessor"></span>
<a name="l00018"></a>00018 <span class="comment">// Error codes</span>
<a name="l00019"></a>00019 <span class="preprocessor">#define RF22_ROUTER_ERROR_NONE              0</span>
<a name="l00020"></a>00020 <span class="preprocessor"></span><span class="preprocessor">#define RF22_ROUTER_ERROR_INVALID_LENGTH    1</span>
<a name="l00021"></a>00021 <span class="preprocessor"></span><span class="preprocessor">#define RF22_ROUTER_ERROR_NO_ROUTE          2</span>
<a name="l00022"></a>00022 <span class="preprocessor"></span><span class="preprocessor">#define RF22_ROUTER_ERROR_TIMEOUT           3</span>
<a name="l00023"></a>00023 <span class="preprocessor"></span><span class="preprocessor">#define RF22_ROUTER_ERROR_NO_REPLY          4</span>
<a name="l00024"></a>00024 <span class="preprocessor"></span><span class="preprocessor">#define RF22_ROUTER_ERROR_UNABLE_TO_DELIVER 5</span>
<a name="l00025"></a>00025 <span class="preprocessor"></span>
<a name="l00026"></a>00026 <span class="comment">// This size of RF22_ROUTER_MAX_MESSAGE_LEN is OK for Arduino Mega, but too big for</span>
<a name="l00027"></a>00027 <span class="comment">// Duemilanova. Size of 50 works with the sample router programs on Duemilanova.</span>
<a name="l00028"></a>00028 <span class="preprocessor">#define RF22_ROUTER_MAX_MESSAGE_LEN (RF22_MAX_MESSAGE_LEN - sizeof(RF22Router::RoutedMessageHeader))</span>
<a name="l00029"></a>00029 <span class="preprocessor"></span><span class="comment">//#define RF22_ROUTER_MAX_MESSAGE_LEN 50</span>
<a name="l00030"></a>00030 
<a name="l00031"></a>00031 <span class="comment">// These allow us to define a simulated network topology for testing purposes</span>
<a name="l00032"></a>00032 <span class="comment">// See RF22Router.cpp for details</span>
<a name="l00033"></a>00033 <span class="comment">//#define RF22_TEST_NETWORK 1</span>
<a name="l00034"></a>00034 <span class="comment">//#define RF22_TEST_NETWORK 2</span>
<a name="l00035"></a>00035 <span class="comment">//#define RF22_TEST_NETWORK 3</span>
<a name="l00036"></a>00036 <span class="comment">//#define RF22_TEST_NETWORK 4</span>
<a name="l00037"></a>00037 <span class="comment"></span>
<a name="l00038"></a>00038 <span class="comment">/////////////////////////////////////////////////////////////////////</span>
<a name="l00039"></a>00039 <span class="comment">/// \class RF22Router RF22Router.h &lt;RF22Router.h&gt;</span>
<a name="l00040"></a>00040 <span class="comment">/// \brief RF22 subclass for sending addressed, optionally acknowledged datagrams</span>
<a name="l00041"></a>00041 <span class="comment">/// multi-hop routed across a network.</span>
<a name="l00042"></a>00042 <span class="comment">///</span>
<a name="l00043"></a>00043 <span class="comment">/// Extends RF22ReliableDatagram to define addressed messages</span>
<a name="l00044"></a>00044 <span class="comment">/// That are reliably transmitted and routed across a network. Each message is transmitted reliably </span>
<a name="l00045"></a>00045 <span class="comment">/// between each hop in order to get from the source node to the destination node.</span>
<a name="l00046"></a>00046 <span class="comment">///</span>
<a name="l00047"></a>00047 <span class="comment">/// With RF22Router, routes are hard wired. This means that each node must have programmed </span>
<a name="l00048"></a>00048 <span class="comment">/// in it how to reach each of the other nodes it will be trying to communicate with. </span>
<a name="l00049"></a>00049 <span class="comment">/// This means you must specify the next-hop node address for each of the destination nodes, </span>
<a name="l00050"></a>00050 <span class="comment">/// using the addRouteTo() function. </span>
<a name="l00051"></a>00051 <span class="comment">///</span>
<a name="l00052"></a>00052 <span class="comment">/// When sendtoWait() is called with a new message to deliver, and the destination address,</span>
<a name="l00053"></a>00053 <span class="comment">/// RF22Router looks up the next hop node for the destination node. It then uses </span>
<a name="l00054"></a>00054 <span class="comment">/// RF22ReliableDatagram to (reliably) deliver the message to the next hop </span>
<a name="l00055"></a>00055 <span class="comment">/// (which is expected also to be running an RF22Router). If that next-hop node is not</span>
<a name="l00056"></a>00056 <span class="comment">/// the final destination, it will also look up the next hop for the destination node and </span>
<a name="l00057"></a>00057 <span class="comment">/// (reliably) deliver the message to the next hop. By this method, messages can be delivered </span>
<a name="l00058"></a>00058 <span class="comment">/// across a network of nodes, even if each node cannot hear all of the others in the network.</span>
<a name="l00059"></a>00059 <span class="comment">/// Each time a message is received for another node and retransmitted to the next hop, </span>
<a name="l00060"></a>00060 <span class="comment">/// the HOPS filed in teh header is incremented. If a message is received for routing to another node </span>
<a name="l00061"></a>00061 <span class="comment">/// which has exceed the routers max_hops, the message wioll be dropped and ignored. </span>
<a name="l00062"></a>00062 <span class="comment">/// This helps prevent infinite routing loops.</span>
<a name="l00063"></a>00063 <span class="comment">///</span>
<a name="l00064"></a>00064 <span class="comment">/// RF22Router supports messages with a dest of RF22_BROADCAST_ADDRESS. Such messages are not routed, </span>
<a name="l00065"></a>00065 <span class="comment">/// and are broadcast (once) to all nodes within range.</span>
<a name="l00066"></a>00066 <span class="comment">///</span>
<a name="l00067"></a>00067 <span class="comment">/// The recvfromAck() function is responsible not just for receiving and delivering </span>
<a name="l00068"></a>00068 <span class="comment">/// messages addressed to this node (or RF22_BROADCAST_ADDRESS), but </span>
<a name="l00069"></a>00069 <span class="comment">/// it is also responsible for routing other message to their next hop. This means that it is important to </span>
<a name="l00070"></a>00070 <span class="comment">/// call recvfromAck() or recvfromAckTimeout() frequently in your main loop. recvfromAck() will return </span>
<a name="l00071"></a>00071 <span class="comment">/// false if it receives a message but it is not for this node.</span>
<a name="l00072"></a>00072 <span class="comment">///</span>
<a name="l00073"></a>00073 <span class="comment">/// RF22Router does not provide reliable end-to-end delivery, but uses reliable hop-to-hop delivery. </span>
<a name="l00074"></a>00074 <span class="comment">/// If a message is unable to be delivered to an end node during to a delivery failure between 2 hops, </span>
<a name="l00075"></a>00075 <span class="comment">/// the source node will not be told about it.</span>
<a name="l00076"></a>00076 <span class="comment">///</span>
<a name="l00077"></a>00077 <span class="comment">/// Note: This class is most useful for networks of nodes that are essentially static </span>
<a name="l00078"></a>00078 <span class="comment">/// (i.e. the nodes dont move around), and for which the </span>
<a name="l00079"></a>00079 <span class="comment">/// routing never changes. If that is not the case for your proposed network, see RF22Mesh instead.</span>
<a name="l00080"></a>00080 <span class="comment">///</span>
<a name="l00081"></a>00081 <span class="comment">/// \par The Routing Table</span>
<a name="l00082"></a>00082 <span class="comment">///</span>
<a name="l00083"></a>00083 <span class="comment">/// The routing table is a local table in RF22Router that holds the information about the next hop node </span>
<a name="l00084"></a>00084 <span class="comment">/// address for each destination address you may want to send a message to. It is your responsibility </span>
<a name="l00085"></a>00085 <span class="comment">/// to make sure every node in an RF22Router network has been configured with a unique address and the </span>
<a name="l00086"></a>00086 <span class="comment">/// routing information so that messages are correctly routed across the network from source node to </span>
<a name="l00087"></a>00087 <span class="comment">/// destination node. This is usually done once in setup() by calling addRouteTo(). </span>
<a name="l00088"></a>00088 <span class="comment">/// The hardwired routing will in general be different on each node, and will depend on the physical </span>
<a name="l00089"></a>00089 <span class="comment">/// topololgy of the network.</span>
<a name="l00090"></a>00090 <span class="comment">/// You can also use addRouteTo() to change a route and </span>
<a name="l00091"></a>00091 <span class="comment">/// deleteRouteTo() to delete a route at run time. Youcan also clear the entire routing table</span>
<a name="l00092"></a>00092 <span class="comment">///</span>
<a name="l00093"></a>00093 <span class="comment">/// The Routing Table has limited capacity for entries (defined by RF22_ROUTING_TABLE_SIZE, which is 10)</span>
<a name="l00094"></a>00094 <span class="comment">/// if more than RF22_ROUTING_TABLE_SIZE are added, the oldest (first) one will be removed by calling </span>
<a name="l00095"></a>00095 <span class="comment">/// retireOldestRoute()</span>
<a name="l00096"></a>00096 <span class="comment">///</span>
<a name="l00097"></a>00097 <span class="comment">/// \par Message Format</span>
<a name="l00098"></a>00098 <span class="comment">///</span>
<a name="l00099"></a>00099 <span class="comment">/// RF22Router add to the lower level RF22ReliableDatagram (and even lower level RF22) class mesage formats. </span>
<a name="l00100"></a>00100 <span class="comment">/// In those lower level classes, the hop-to-hop message headers are in the RF22 message headers, </span>
<a name="l00101"></a>00101 <span class="comment">/// and are handled automcatically by tyhe RF22 hardware.</span>
<a name="l00102"></a>00102 <span class="comment">/// RF22Router and its subclasses add an end-to-end addressing header in the payload of the RF22 message, </span>
<a name="l00103"></a>00103 <span class="comment">/// and before the RF22Router application data.</span>
<a name="l00104"></a>00104 <span class="comment">/// - 1 octet DEST, the destination node address (ie the address of the final </span>
<a name="l00105"></a>00105 <span class="comment">///   destination node for this message)</span>
<a name="l00106"></a>00106 <span class="comment">/// - 1 octet SOURCE, the source node address (ie the address of the originating node that first sent </span>
<a name="l00107"></a>00107 <span class="comment">///   the message).</span>
<a name="l00108"></a>00108 <span class="comment">/// - 1 octet HOPS, the number of hops this message has traversed so far.</span>
<a name="l00109"></a>00109 <span class="comment">/// - 1 octet ID, an incrementing message ID for end-to-end message tracking for use by subclasses. </span>
<a name="l00110"></a>00110 <span class="comment">///   Not used by RF22Router.</span>
<a name="l00111"></a>00111 <span class="comment">/// - 1 octet FLAGS, a bitmask for use by subclasses. Not used by RF22Router.</span>
<a name="l00112"></a>00112 <span class="comment">/// - 0 or more octets DATA, the application payload data. The length of this data is implicit </span>
<a name="l00113"></a>00113 <span class="comment">///   in the length of the entire message.</span>
<a name="l00114"></a>00114 <span class="comment">///</span>
<a name="l00115"></a>00115 <span class="comment">/// You should be careful to note that there are ID and FLAGS fields in the low level per-hop </span>
<a name="l00116"></a>00116 <span class="comment">/// message header too. These are used only for hop-to-hop, and in general will be different to </span>
<a name="l00117"></a>00117 <span class="comment">/// the ones at the RF22Router level.</span>
<a name="l00118"></a>00118 <span class="comment">///</span>
<a name="l00119"></a>00119 <span class="comment">/// \par Testing</span>
<a name="l00120"></a>00120 <span class="comment">///</span>
<a name="l00121"></a>00121 <span class="comment">/// Bench testing of such networks is notoriously difficult, especially simulating limited radio </span>
<a name="l00122"></a>00122 <span class="comment">/// connectivity between some nodes.</span>
<a name="l00123"></a>00123 <span class="comment">/// To assist testing (both during RF22 development and for your own networks) </span>
<a name="l00124"></a>00124 <span class="comment">/// RF22Router.cpp has the ability to </span>
<a name="l00125"></a>00125 <span class="comment">/// simulate a number of different small network topologies. Each simulated network supports 4 nodes with </span>
<a name="l00126"></a>00126 <span class="comment">/// addresses 1 to 4. It operates by pretending to not hear RF22 messages from certain other nodes.</span>
<a name="l00127"></a>00127 <span class="comment">/// You can enable testing with a \#define TEST_NETWORK in RF22Router.h</span>
<a name="l00128"></a>00128 <span class="comment">/// The sample programs rf22_mesh_* rely on this feature.</span>
<a name="l00129"></a>00129 <span class="comment">///</span>
<a name="l00130"></a>00130 <span class="comment">/// Part of the Arduino RF22 library for operating with HopeRF RF22 compatible transceivers </span>
<a name="l00131"></a>00131 <span class="comment">/// (see http://www.hoperf.com)</span>
<a name="l00132"></a><a class="code" href="classRF22Router.html">00132</a> <span class="comment"></span><span class="keyword">class </span><a class="code" href="classRF22Router.html" title="RF22 subclass for sending addressed, optionally acknowledged datagrams multi-hop routed across a netw...">RF22Router</a> : <span class="keyword">public</span> <a class="code" href="classRF22ReliableDatagram.html" title="RF22 subclass for sending addressed, acknowledged, retransmitted datagrams.">RF22ReliableDatagram</a>
<a name="l00133"></a>00133 {
<a name="l00134"></a>00134 <span class="keyword">public</span>:
<a name="l00135"></a>00135 <span class="comment"></span>
<a name="l00136"></a>00136 <span class="comment">    /// Defines the structure of the RF22Router message header, used to keep track of end-to-end delivery</span>
<a name="l00137"></a>00137 <span class="comment">    /// parameters</span>
<a name="l00138"></a><a class="code" href="structRF22Router_1_1RoutedMessageHeader.html">00138</a> <span class="comment"></span>    <span class="keyword">typedef</span> <span class="keyword">struct</span>
<a name="l00139"></a>00139     {
<a name="l00140"></a><a class="code" href="structRF22Router_1_1RoutedMessageHeader.html#ac87393d376d884f770ee65764911f216">00140</a>         uint8_t    <a class="code" href="structRF22Router_1_1RoutedMessageHeader.html#ac87393d376d884f770ee65764911f216" title="Destination node address.">dest</a>;       <span class="comment">///&lt; Destination node address</span>
<a name="l00141"></a><a class="code" href="structRF22Router_1_1RoutedMessageHeader.html#a69f76048bb061a95aafdab043a6b5c7f">00141</a> <span class="comment"></span>        uint8_t    <a class="code" href="structRF22Router_1_1RoutedMessageHeader.html#a69f76048bb061a95aafdab043a6b5c7f" title="Originator node address.">source</a>;     <span class="comment">///&lt; Originator node address</span>
<a name="l00142"></a><a class="code" href="structRF22Router_1_1RoutedMessageHeader.html#a4c28e31c2768c519a1f5e01808f0220d">00142</a> <span class="comment"></span>        uint8_t    <a class="code" href="structRF22Router_1_1RoutedMessageHeader.html#a4c28e31c2768c519a1f5e01808f0220d" title="Hops traversed so far.">hops</a>;       <span class="comment">///&lt; Hops traversed so far</span>
<a name="l00143"></a><a class="code" href="structRF22Router_1_1RoutedMessageHeader.html#a7849aef5380c9fd8f597fa1e4028563e">00143</a> <span class="comment"></span>        uint8_t    <a class="code" href="structRF22Router_1_1RoutedMessageHeader.html#a7849aef5380c9fd8f597fa1e4028563e" title="Originator sequence number.">id</a>;         <span class="comment">///&lt; Originator sequence number</span>
<a name="l00144"></a><a class="code" href="structRF22Router_1_1RoutedMessageHeader.html#a958cb0cdf8d3731495590088ea448943">00144</a> <span class="comment"></span>        uint8_t    <a class="code" href="structRF22Router_1_1RoutedMessageHeader.html#a958cb0cdf8d3731495590088ea448943" title="Originator flags.">flags</a>;      <span class="comment">///&lt; Originator flags</span>
<a name="l00145"></a>00145 <span class="comment"></span>        <span class="comment">// Data follows, Length is implicit in the overall message length</span>
<a name="l00146"></a>00146     } <a class="code" href="structRF22Router_1_1RoutedMessageHeader.html">RoutedMessageHeader</a>;
<a name="l00147"></a>00147 <span class="comment"></span>
<a name="l00148"></a>00148 <span class="comment">    /// Defines the structure of a RF22Router message</span>
<a name="l00149"></a><a class="code" href="structRF22Router_1_1RoutedMessage.html">00149</a> <span class="comment"></span>    <span class="keyword">typedef</span> <span class="keyword">struct</span>
<a name="l00150"></a>00150     {
<a name="l00151"></a><a class="code" href="structRF22Router_1_1RoutedMessage.html#a730465e2760dc904d3bbfd52bf2cd1f4">00151</a>         <a class="code" href="structRF22Router_1_1RoutedMessageHeader.html">RoutedMessageHeader</a> <a class="code" href="structRF22Router_1_1RoutedMessage.html#a730465e2760dc904d3bbfd52bf2cd1f4" title="end-to-end delivery header">header</a>;    <span class="comment">///&lt; end-to-end delivery header</span>
<a name="l00152"></a><a class="code" href="structRF22Router_1_1RoutedMessage.html#a65c73cedebd25552a31c88aa4c6aa1f4">00152</a> <span class="comment"></span>        uint8_t             data[RF22_ROUTER_MAX_MESSAGE_LEN]; <span class="comment">///&lt; Applicaiton payload data</span>
<a name="l00153"></a>00153 <span class="comment"></span>    } <a class="code" href="structRF22Router_1_1RoutedMessage.html" title="Defines the structure of a RF22Router message.">RoutedMessage</a>;
<a name="l00154"></a>00154 <span class="comment"></span>
<a name="l00155"></a>00155 <span class="comment">    /// Values for the possible states for routes</span>
<a name="l00156"></a><a class="code" href="classRF22Router.html#a415a311041d63a34c1d055c706cf872f">00156</a> <span class="comment"></span>    <span class="keyword">typedef</span> <span class="keyword">enum</span>
<a name="l00157"></a>00157     {
<a name="l00158"></a><a class="code" href="classRF22Router.html#a415a311041d63a34c1d055c706cf872fa9d8af0e15d87751008004e31b3db1d80">00158</a>         <a class="code" href="classRF22Router.html#a415a311041d63a34c1d055c706cf872fa9d8af0e15d87751008004e31b3db1d80" title="No valid route is known.">Invalid</a> = 0,           <span class="comment">///&lt; No valid route is known</span>
<a name="l00159"></a><a class="code" href="classRF22Router.html#a415a311041d63a34c1d055c706cf872fa7f5766dd7d24e2607d7d7d8d7afc246b">00159</a> <span class="comment"></span>        <a class="code" href="classRF22Router.html#a415a311041d63a34c1d055c706cf872fa7f5766dd7d24e2607d7d7d8d7afc246b" title="Discovering a route (not currently used)">Discovering</a>,           <span class="comment">///&lt; Discovering a route (not currently used)</span>
<a name="l00160"></a><a class="code" href="classRF22Router.html#a415a311041d63a34c1d055c706cf872fad754c46fbf5c01d1c70eef6a05987109">00160</a> <span class="comment"></span>        <a class="code" href="classRF22Router.html#a415a311041d63a34c1d055c706cf872fad754c46fbf5c01d1c70eef6a05987109" title="Route is valid.">Valid</a>                  <span class="comment">///&lt; Route is valid</span>
<a name="l00161"></a>00161 <span class="comment"></span>    } <a class="code" href="classRF22Router.html#a415a311041d63a34c1d055c706cf872f" title="Values for the possible states for routes.">RouteState</a>;
<a name="l00162"></a>00162 <span class="comment"></span>
<a name="l00163"></a>00163 <span class="comment">    /// Defines an entry in the routing table</span>
<a name="l00164"></a><a class="code" href="structRF22Router_1_1RoutingTableEntry.html">00164</a> <span class="comment"></span>    <span class="keyword">typedef</span> <span class="keyword">struct</span>
<a name="l00165"></a>00165     {
<a name="l00166"></a><a class="code" href="structRF22Router_1_1RoutingTableEntry.html#ae242ebbb95585705b02d1799bed18622">00166</a>         uint8_t      <a class="code" href="structRF22Router_1_1RoutingTableEntry.html#ae242ebbb95585705b02d1799bed18622" title="Destination node address.">dest</a>;      <span class="comment">///&lt; Destination node address</span>
<a name="l00167"></a><a class="code" href="structRF22Router_1_1RoutingTableEntry.html#a304700b8d7f20371e53a1da2c6ab2c82">00167</a> <span class="comment"></span>        uint8_t      <a class="code" href="structRF22Router_1_1RoutingTableEntry.html#a304700b8d7f20371e53a1da2c6ab2c82" title="Send via this next hop address.">next_hop</a>;  <span class="comment">///&lt; Send via this next hop address</span>
<a name="l00168"></a><a class="code" href="structRF22Router_1_1RoutingTableEntry.html#a7f8fb79413e6690eacf387fa0d3512fb">00168</a> <span class="comment"></span>        uint8_t      <a class="code" href="structRF22Router_1_1RoutingTableEntry.html#a7f8fb79413e6690eacf387fa0d3512fb" title="State of this route, one of RouteState.">state</a>;     <span class="comment">///&lt; State of this route, one of RouteState</span>
<a name="l00169"></a>00169 <span class="comment"></span>    } <a class="code" href="structRF22Router_1_1RoutingTableEntry.html" title="Defines an entry in the routing table.">RoutingTableEntry</a>;
<a name="l00170"></a>00170 <span class="comment"></span>
<a name="l00171"></a>00171 <span class="comment">    /// Constructor. </span>
<a name="l00172"></a>00172 <span class="comment">    /// \param[in] thisAddress The address to assign to this node. Defaults to 0</span>
<a name="l00173"></a>00173 <span class="comment">    /// \param[in] slaveSelectPin the Arduino pin number of the output to use to select the RF22 before</span>
<a name="l00174"></a>00174 <span class="comment">    /// accessing it</span>
<a name="l00175"></a>00175 <span class="comment">    /// \param[in] interrupt The interrupt number to use. Default is interrupt 0 (Arduino input pin 2)</span>
<a name="l00176"></a>00176 <span class="comment"></span>    <a class="code" href="classRF22Router.html#a5e934a0ccc2d764ef0c0aa70a3d32cf9">RF22Router</a>(uint8_t thisAddress = 0, uint8_t slaveSelectPin = 10, uint8_t interrupt = 0);
<a name="l00177"></a>00177 <span class="comment"></span>
<a name="l00178"></a>00178 <span class="comment">    /// Initialises this instance and the radio module connected to it.</span>
<a name="l00179"></a>00179 <span class="comment">    /// Overrides the init() function in RF22.</span>
<a name="l00180"></a>00180 <span class="comment">    /// Sets max_hops to the default of RF22_DEFAULT_MAX_HOPS (30)</span>
<a name="l00181"></a>00181 <span class="comment"></span>    <span class="keywordtype">boolean</span> <a class="code" href="classRF22Router.html#ac6037c3300511d0277e42edea8ccf873">init</a>();
<a name="l00182"></a>00182 <span class="comment"></span>
<a name="l00183"></a>00183 <span class="comment">    /// Sets the max_hops to the given value</span>
<a name="l00184"></a>00184 <span class="comment">    /// This controls the maximum number of hops allowed between source and destination nodes</span>
<a name="l00185"></a>00185 <span class="comment">    /// Messages that are not delivered by the time their HOPS field exceeds max_hops on a </span>
<a name="l00186"></a>00186 <span class="comment">    /// routing node will be dropped and ignored.</span>
<a name="l00187"></a>00187 <span class="comment">    /// \param [in] max_hops The new value for max_hops</span>
<a name="l00188"></a>00188 <span class="comment"></span>    <span class="keywordtype">void</span> <a class="code" href="classRF22Router.html#a05f3bc34b320c05af36ad0f0f927bfa0">setMaxHops</a>(uint8_t max_hops);
<a name="l00189"></a>00189 <span class="comment"></span>
<a name="l00190"></a>00190 <span class="comment">    /// Adds a route to the local routing table, or updates it if already present.</span>
<a name="l00191"></a>00191 <span class="comment">    /// If there is not enough room the oldest (first) route will be deleted by calling retireOldestRoute().</span>
<a name="l00192"></a>00192 <span class="comment">    /// \param [in] dest The destination node address. RF22_BROADCAST_ADDRESS is permitted.</span>
<a name="l00193"></a>00193 <span class="comment">    /// \param [in] next_hop The address of the next hop to send messages destined for dest</span>
<a name="l00194"></a>00194 <span class="comment">    /// \param [in] state The satte of the route. Defaults to Valid</span>
<a name="l00195"></a>00195 <span class="comment"></span>    <span class="keywordtype">void</span> <a class="code" href="classRF22Router.html#aa212a84ed49696f208e513dfbc87813e">addRouteTo</a>(uint8_t dest, uint8_t next_hop, uint8_t state = <a class="code" href="classRF22Router.html#a415a311041d63a34c1d055c706cf872fad754c46fbf5c01d1c70eef6a05987109" title="Route is valid.">Valid</a>);
<a name="l00196"></a>00196 <span class="comment"></span>
<a name="l00197"></a>00197 <span class="comment">    /// Finds and returns a RoutingTableEntry for the given destination node</span>
<a name="l00198"></a>00198 <span class="comment">    /// \param [in] dest The desired destination node address.</span>
<a name="l00199"></a>00199 <span class="comment">    /// \return pointer to a RoutingTableEntry for dest</span>
<a name="l00200"></a>00200 <span class="comment"></span>    <a class="code" href="structRF22Router_1_1RoutingTableEntry.html" title="Defines an entry in the routing table.">RoutingTableEntry</a>* <a class="code" href="classRF22Router.html#a33862cdecebe61b34ee93567db3c673b">getRouteTo</a>(uint8_t dest);
<a name="l00201"></a>00201 <span class="comment"></span>
<a name="l00202"></a>00202 <span class="comment">    /// Deletes from the local routing table any route for the destination node.</span>
<a name="l00203"></a>00203 <span class="comment">    /// \param [in] dest The destination node address</span>
<a name="l00204"></a>00204 <span class="comment">    /// \return true if the route was present</span>
<a name="l00205"></a>00205 <span class="comment"></span>    <span class="keywordtype">boolean</span> <a class="code" href="classRF22Router.html#a012322d33591b95adde3a206dd618ffe">deleteRouteTo</a>(uint8_t dest);
<a name="l00206"></a>00206 <span class="comment"></span>
<a name="l00207"></a>00207 <span class="comment">    /// Deletes the oldest (first) route from the </span>
<a name="l00208"></a>00208 <span class="comment">    /// local routing table</span>
<a name="l00209"></a>00209 <span class="comment"></span>    <span class="keywordtype">void</span> <a class="code" href="classRF22Router.html#a63c6746df7f09ec825760bebb1581cb4">retireOldestRoute</a>();
<a name="l00210"></a>00210 <span class="comment"></span>
<a name="l00211"></a>00211 <span class="comment">    /// Clears all entries from the </span>
<a name="l00212"></a>00212 <span class="comment">    /// local routing table</span>
<a name="l00213"></a>00213 <span class="comment"></span>    <span class="keywordtype">void</span> <a class="code" href="classRF22Router.html#aaabe7e6d549d3f41e8071f1952b847d3">clearRoutingTable</a>();
<a name="l00214"></a>00214 
<a name="l00215"></a>00215 <span class="preprocessor">#ifdef RF22_HAVE_SERIAL</span>
<a name="l00216"></a>00216 <span class="preprocessor"></span><span class="comment">    /// If RF22_HAVE_SERIAL is defined, this will print out the contents of the local </span>
<a name="l00217"></a>00217 <span class="comment"></span><span class="comment">    /// routing table using Serial</span>
<a name="l00218"></a>00218 <span class="comment"></span>    <span class="keywordtype">void</span> printRoutingTable();
<a name="l00219"></a>00219 <span class="preprocessor">#endif</span>
<a name="l00220"></a>00220 <span class="preprocessor"></span><span class="comment"></span>
<a name="l00221"></a>00221 <span class="comment">    /// Sends a message to the destination node. Initialises the RF22Router message header </span>
<a name="l00222"></a>00222 <span class="comment">    /// (the SOURCE address is set to the address of this node, HOPS to 0) and calls </span>
<a name="l00223"></a>00223 <span class="comment">    /// route() which looks up in the routing table the next hop to deliver to and sends the </span>
<a name="l00224"></a>00224 <span class="comment">    /// message to the next hop. Waits for an acknowledgement from the next hop </span>
<a name="l00225"></a>00225 <span class="comment">    /// (but not from the destination node (if that is different).</span>
<a name="l00226"></a>00226 <span class="comment">    /// \param [in] buf The application message data</span>
<a name="l00227"></a>00227 <span class="comment">    /// \param [in] len Number of octets in the application message data. 0 is permitted</span>
<a name="l00228"></a>00228 <span class="comment">    /// \param [in] dest The destination node address</span>
<a name="l00229"></a>00229 <span class="comment">    /// \return The result code:</span>
<a name="l00230"></a>00230 <span class="comment">    ///         - RF22_ROUTER_ERROR_NONE Message was routed and deliverd to the next hop </span>
<a name="l00231"></a>00231 <span class="comment">    ///           (not necessarily to the final dest address)</span>
<a name="l00232"></a>00232 <span class="comment">    ///         - RF22_ROUTER_ERROR_NO_ROUTE There was no route for dest in the local routing table</span>
<a name="l00233"></a>00233 <span class="comment">    ///         - RF22_ROUTER_ERROR_UNABLE_TO_DELIVER Noyt able to deliver to the next hop </span>
<a name="l00234"></a>00234 <span class="comment">    ///           (usually because it dod not acknowledge due to being off the air or out of range</span>
<a name="l00235"></a>00235 <span class="comment"></span>    uint8_t <a class="code" href="classRF22Router.html#adb03b403f981659ea55e90867741d085">sendtoWait</a>(uint8_t* buf, uint8_t len, uint8_t dest);
<a name="l00236"></a>00236 <span class="comment"></span>
<a name="l00237"></a>00237 <span class="comment">    /// Similar to sendtoWait() above, but spoofs the source address.</span>
<a name="l00238"></a>00238 <span class="comment">    /// For internal use only during routing</span>
<a name="l00239"></a>00239 <span class="comment">    /// \param [in] buf The application message data</span>
<a name="l00240"></a>00240 <span class="comment">    /// \param [in] len Number of octets in the application message data. 0 is permitted</span>
<a name="l00241"></a>00241 <span class="comment">    /// \param [in] dest The destination node address</span>
<a name="l00242"></a>00242 <span class="comment">    /// \param [in] source The (fake) originatong node address.</span>
<a name="l00243"></a>00243 <span class="comment">    /// \return The result code:</span>
<a name="l00244"></a>00244 <span class="comment">    ///         - RF22_ROUTER_ERROR_NONE Message was routed and deliverd to the next hop </span>
<a name="l00245"></a>00245 <span class="comment">    ///           (not necessarily to the final dest address)</span>
<a name="l00246"></a>00246 <span class="comment">    ///         - RF22_ROUTER_ERROR_NO_ROUTE There was no route for dest in the local routing table</span>
<a name="l00247"></a>00247 <span class="comment">    ///         - RF22_ROUTER_ERROR_UNABLE_TO_DELIVER Noyt able to deliver to the next hop </span>
<a name="l00248"></a>00248 <span class="comment">    ///           (usually because it dod not acknowledge due to being off the air or out of range</span>
<a name="l00249"></a>00249 <span class="comment"></span>    uint8_t <a class="code" href="classRF22Router.html#adb03b403f981659ea55e90867741d085">sendtoWait</a>(uint8_t* buf, uint8_t len, uint8_t dest, uint8_t source);
<a name="l00250"></a>00250 <span class="comment"></span>
<a name="l00251"></a>00251 <span class="comment">    /// Starts the receiver if it is not running already.</span>
<a name="l00252"></a>00252 <span class="comment">    /// If there is a valid message available for this node (or RF22_BROADCAST_ADDRESS), </span>
<a name="l00253"></a>00253 <span class="comment">    /// send an acknowledgement to the last hop</span>
<a name="l00254"></a>00254 <span class="comment">    /// address (blocking until this is complete), then copy the application message payload data</span>
<a name="l00255"></a>00255 <span class="comment">    /// to buf and return true</span>
<a name="l00256"></a>00256 <span class="comment">    /// else return false. </span>
<a name="l00257"></a>00257 <span class="comment">    /// If a message is copied, *len is set to the length..</span>
<a name="l00258"></a>00258 <span class="comment">    /// If from is not NULL, the originator SOURCE address is placed in *source.</span>
<a name="l00259"></a>00259 <span class="comment">    /// If to is not NULL, the DEST address is placed in *dest. This might be this nodes address or </span>
<a name="l00260"></a>00260 <span class="comment">    /// RF22_BROADCAST_ADDRESS. </span>
<a name="l00261"></a>00261 <span class="comment">    /// This is the preferred function for getting messages addressed to this node.</span>
<a name="l00262"></a>00262 <span class="comment">    /// If the message is not a broadcast, acknowledge to the sender before returning.</span>
<a name="l00263"></a>00263 <span class="comment">    /// \param[in] buf Location to copy the received message</span>
<a name="l00264"></a>00264 <span class="comment">    /// \param[in,out] len Available space in buf. Set to the actual number of octets copied.</span>
<a name="l00265"></a>00265 <span class="comment">    /// \param[in] source If present and not NULL, the referenced uint8_t will be set to the SOURCE address</span>
<a name="l00266"></a>00266 <span class="comment">    /// \param[in] dest If present and not NULL, the referenced uint8_t will be set to the DEST address</span>
<a name="l00267"></a>00267 <span class="comment">    /// \param[in] id If present and not NULL, the referenced uint8_t will be set to the ID</span>
<a name="l00268"></a>00268 <span class="comment">    /// \param[in] flags If present and not NULL, the referenced uint8_t will be set to the FLAGS</span>
<a name="l00269"></a>00269 <span class="comment">    /// (not just those addressed to this node).</span>
<a name="l00270"></a>00270 <span class="comment">    /// \return true if a valid message was recvived for this node copied to buf</span>
<a name="l00271"></a>00271 <span class="comment"></span>    <span class="keywordtype">boolean</span> <a class="code" href="classRF22Router.html#abbb4344322693607d8a0be5f0f831015">recvfromAck</a>(uint8_t* buf, uint8_t* len, uint8_t* source = NULL, uint8_t* dest = NULL, uint8_t* <span class="keywordtype">id</span> = NULL, uint8_t* flags = NULL);
<a name="l00272"></a>00272 <span class="comment"></span>
<a name="l00273"></a>00273 <span class="comment">    /// Starts the receiver if it is not running already.</span>
<a name="l00274"></a>00274 <span class="comment">    /// Similar to recvfromAck(), this will block until either a valid message available for this node</span>
<a name="l00275"></a>00275 <span class="comment">    /// or the timeout expires. </span>
<a name="l00276"></a>00276 <span class="comment">    /// \param[in] buf Location to copy the received message</span>
<a name="l00277"></a>00277 <span class="comment">    /// \param[in,out] len Available space in buf. Set to the actual number of octets copied.</span>
<a name="l00278"></a>00278 <span class="comment">    /// \param[in] timeout Maximum time to wait in milliseconds</span>
<a name="l00279"></a>00279 <span class="comment">    /// \param[in] source If present and not NULL, the referenced uint8_t will be set to the SOURCE address</span>
<a name="l00280"></a>00280 <span class="comment">    /// \param[in] dest If present and not NULL, the referenced uint8_t will be set to the DEST address</span>
<a name="l00281"></a>00281 <span class="comment">    /// \param[in] id If present and not NULL, the referenced uint8_t will be set to the ID</span>
<a name="l00282"></a>00282 <span class="comment">    /// \param[in] flags If present and not NULL, the referenced uint8_t will be set to the FLAGS</span>
<a name="l00283"></a>00283 <span class="comment">    /// (not just those addressed to this node).</span>
<a name="l00284"></a>00284 <span class="comment">    /// \return true if a valid message was copied to buf</span>
<a name="l00285"></a>00285 <span class="comment"></span>    <span class="keywordtype">boolean</span> <a class="code" href="classRF22Router.html#af4f975c229288c480e13de4e6ddea8fb">recvfromAckTimeout</a>(uint8_t* buf, uint8_t* len,  uint16_t timeout, uint8_t* source = NULL, uint8_t* dest = NULL, uint8_t* <span class="keywordtype">id</span> = NULL, uint8_t* flags = NULL);
<a name="l00286"></a>00286 
<a name="l00287"></a>00287 <span class="keyword">protected</span>:
<a name="l00288"></a>00288 <span class="comment"></span>
<a name="l00289"></a>00289 <span class="comment">    /// Lets sublasses peek at messages going </span>
<a name="l00290"></a>00290 <span class="comment">    /// past before routing or local delivery.</span>
<a name="l00291"></a>00291 <span class="comment">    /// Called by recvfromAck() immediately after it gets the message from RF22ReliableDatagram</span>
<a name="l00292"></a>00292 <span class="comment">    /// \param [in] message Pointer to the RF22Router message that was received.</span>
<a name="l00293"></a>00293 <span class="comment">    /// \param [in] messageLen Length of message in octets</span>
<a name="l00294"></a>00294 <span class="comment"></span>    <span class="keyword">virtual</span> <span class="keywordtype">void</span> <a class="code" href="classRF22Router.html#a1ddc1e959c8851add1c78ee1b1af8b66">peekAtMessage</a>(<a class="code" href="structRF22Router_1_1RoutedMessage.html" title="Defines the structure of a RF22Router message.">RoutedMessage</a>* message, uint8_t messageLen);
<a name="l00295"></a>00295 <span class="comment"></span>
<a name="l00296"></a>00296 <span class="comment">    /// Finds the next-hop route and sends the message via RF22ReliableDatagram::sendtoWait().</span>
<a name="l00297"></a>00297 <span class="comment">    /// This is virtual, which lets subclasses override or intercept the route() function.</span>
<a name="l00298"></a>00298 <span class="comment">    /// Called by sendtoWait after the message header has been filled in.</span>
<a name="l00299"></a>00299 <span class="comment">    /// \param [in] message Pointer to the RF22Router message to be sent.</span>
<a name="l00300"></a>00300 <span class="comment">    /// \param [in] messageLen Length of message in octets</span>
<a name="l00301"></a>00301 <span class="comment"></span>    <span class="keyword">virtual</span> uint8_t <a class="code" href="classRF22Router.html#a76edfc892808e5b8c1d54f69ba2d8ebd">route</a>(<a class="code" href="structRF22Router_1_1RoutedMessage.html" title="Defines the structure of a RF22Router message.">RoutedMessage</a>* message, uint8_t messageLen);
<a name="l00302"></a>00302 <span class="comment"></span>
<a name="l00303"></a>00303 <span class="comment">    /// Deletes a specific rout entry from therouting table</span>
<a name="l00304"></a>00304 <span class="comment">    /// \param [in] index The 0 based index of the routing table entry to delete</span>
<a name="l00305"></a>00305 <span class="comment"></span>    <span class="keywordtype">void</span> <a class="code" href="classRF22Router.html#ac8bf56e995cbf6e51104f888496751a8">deleteRoute</a>(uint8_t index);
<a name="l00306"></a>00306 <span class="comment"></span>
<a name="l00307"></a>00307 <span class="comment">    /// The last end-to-end sequence number to be used</span>
<a name="l00308"></a>00308 <span class="comment">    /// Defaults to 0</span>
<a name="l00309"></a><a class="code" href="classRF22Router.html#a27591173bc7e622beae9c247634b3dec">00309</a> <span class="comment"></span>    uint8_t <a class="code" href="classRF22Router.html#a27591173bc7e622beae9c247634b3dec">_lastE2ESequenceNumber</a>;
<a name="l00310"></a>00310 <span class="comment"></span>
<a name="l00311"></a>00311 <span class="comment">    /// The maximum number of hops permitted in routed messages.</span>
<a name="l00312"></a>00312 <span class="comment">    /// If a routed message would exceed this number of hops it is dropped and ignored.</span>
<a name="l00313"></a><a class="code" href="classRF22Router.html#ab6af5059286ee412f81f33c67ac95132">00313</a> <span class="comment"></span>    uint8_t              <a class="code" href="classRF22Router.html#ab6af5059286ee412f81f33c67ac95132">_max_hops</a>;
<a name="l00314"></a>00314 
<a name="l00315"></a>00315 <span class="keyword">private</span>:
<a name="l00316"></a>00316 <span class="comment"></span>
<a name="l00317"></a>00317 <span class="comment">    /// Temporary mesage buffer</span>
<a name="l00318"></a>00318 <span class="comment"></span>    <span class="keyword">static</span> <a class="code" href="structRF22Router_1_1RoutedMessage.html" title="Defines the structure of a RF22Router message.">RoutedMessage</a> _tmpMessage;
<a name="l00319"></a>00319 <span class="comment"></span>
<a name="l00320"></a>00320 <span class="comment">    /// Local routing table</span>
<a name="l00321"></a>00321 <span class="comment"></span>    <a class="code" href="structRF22Router_1_1RoutingTableEntry.html" title="Defines an entry in the routing table.">RoutingTableEntry</a>    _routes[RF22_ROUTING_TABLE_SIZE];
<a name="l00322"></a>00322 };
<a name="l00323"></a>00323 
<a name="l00324"></a>00324 <span class="preprocessor">#endif</span>
<a name="l00325"></a>00325 <span class="preprocessor"></span>
</pre></div></div>
</div>


<hr class="footer"/><address class="footer"><small>
Generated by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.7.5.1
</small></address>

</body>
</html>
